1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.fhw.cabaweb.tools;
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import java.util.Date;
26 import java.util.Properties;
27 import java.util.Vector;
28
29 import javax.activation.DataHandler;
30 import javax.activation.FileDataSource;
31
32 import javax.mail.AuthenticationFailedException;
33 import javax.mail.Message;
34 import javax.mail.MessagingException;
35 import javax.mail.Part;
36 import javax.mail.SendFailedException;
37 import javax.mail.Session;
38 import javax.mail.Transport;
39
40 import javax.mail.internet.AddressException;
41 import javax.mail.internet.InternetAddress;
42 import javax.mail.internet.MimeBodyPart;
43 import javax.mail.internet.MimeMessage;
44 import javax.mail.internet.MimeMultipart;
45
46 /***
47 * Klasse um verschiedene mit eMail zusammenhängende Funktionen zusammenzufassen
48 *
49 * @author <a href="mailto:thomas.vogt@tvc-software.com">Thomas Vogt</a>
50 * @version Version 1.0 08.07.2004
51 */
52 public final class MailUtilities
53 {
54 /***
55 * Standardkonstruktor der nicht zugänglich sein soll, da dies eine Utility Klasse ist
56 */
57 protected MailUtilities()
58 {
59 throw new UnsupportedOperationException();
60 }
61
62 /***
63 * Bereitet den Nachrichtenheader anhand der übergebenen Paramter auf.
64 * Based on a JavaMail Wrapper example from http://w3.iac.net/~crawford/ .
65 *
66 * @param session Die zu nutzende Session
67 * @param from Der Absender (Absenderadresse)
68 * @param to Der Adressat (Zieladresse)
69 * @param cc Adressat einer CarbonCopy (CC Adresse)
70 * @param subject Der Betreff
71 *
72 * @return Der fertig gepackte Nachrichtenheader
73 *
74 * @throws IOException
75 * @throws AddressException
76 * @throws MessagingException
77 */
78 protected static Message prepareHeader(Session session, String from, String to, String cc, String subject) throws IOException, AddressException, MessagingException
79 {
80 Message msg = new MimeMessage(session);
81
82 InternetAddress from_addr = new InternetAddress(from);
83 InternetAddress to_addr = new InternetAddress(to);
84 InternetAddress cc_addr = new InternetAddress();
85
86 if (cc != null && cc != "")
87 cc_addr = new InternetAddress(cc);
88
89 msg.setFrom(from_addr);
90 msg.addRecipients(Message.RecipientType.TO, new InternetAddress[] { to_addr });
91
92 if (cc != null && cc != "")
93 msg.addRecipients(Message.RecipientType.CC, new InternetAddress[] { cc_addr });
94
95 msg.setSubject(subject.trim());
96 msg.setSentDate(new Date());
97 msg.setHeader("X-Mailer", "CabaWeb JavaMail");
98 msg.setHeader("content-type", "text/plain");
99
100 return msg;
101 }
102
103 /***
104 * Versendet eine eMail über einen SMTP Server.
105 * Based on a JavaMail Wrapper example from http://w3.iac.net/~crawford/ .
106 *
107 * @param smtp_host Der Name des zu verwendenenden SMTP Servers
108 * @param smtp_port Der Port des zu verwendenenden SMTP Servers
109 * @param smtp_user Der Benutzername für den Versand über den SMTP Server
110 * @param smtp_password Das Passwort für den Versand über den SMTP Server
111 * @param from Der Absender (Absenderadresse)
112 * @param to Der Adressat (Zieladresse)
113 * @param cc Adressat einer CarbonCopy (CC Adresse)
114 * @param subject Der Betreff
115 * @param message Der Inhalt
116 *
117 * @throws IOException
118 * @throws AddressException
119 * @throws MessagingException
120 * @throws AuthenticationFailedException
121 * @throws SendFailedException
122 */
123 public static final void sendMail(String smtp_host, String smtp_port, String smtp_user, String smtp_password, String from, String to, String cc, String subject, String message) throws IOException, AddressException, MessagingException, AuthenticationFailedException, SendFailedException
124 {
125 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
126
127 String proxyHost = properties.getProperty("ProxyHost");
128 String proxyPort = properties.getProperty("ProxyPort");
129
130 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
131 {
132 Properties systemSettings = System.getProperties();
133 systemSettings.put("proxySet", "true");
134 systemSettings.put("proxyHost", proxyHost);
135 systemSettings.put("proxyPort", proxyPort);
136 System.setProperties(systemSettings);
137 }
138
139 Properties props = new Properties();
140
141 if(smtp_user == null)
142 smtp_user = "";
143 if(smtp_password == null)
144 smtp_password = "";
145 if(smtp_host == null)
146 smtp_host = "";
147 if(smtp_port == null)
148 smtp_port = "";
149
150 if(smtp_user != null && smtp_user != "" && smtp_password != null && smtp_password != "" )
151 props.put("mail.smtp.auth", "true");
152 props.put("mail.smtp.localhost", "CabaWeb Server");
153 props.put("mail.smtp.user", smtp_user.trim());
154 props.put("mail.smtp.host", smtp_host.trim());
155 props.put("mail.smtp.port", smtp_port.trim());
156 Session session = Session.getDefaultInstance(props, null);
157
158 Message msg = prepareHeader(session, from, to, cc, subject);
159 msg.setContent(message, "text/plain");
160 msg.saveChanges();
161
162 Transport bus = session.getTransport("smtp");
163
164 if (smtp_user == null || smtp_password == null || smtp_user.trim() == "" || smtp_password.trim() == "")
165 bus.connect();
166 else
167 bus.connect(smtp_host.trim(), Integer.parseInt(smtp_port.trim()), smtp_user.trim(), smtp_password.trim());
168
169 bus.sendMessage(msg, msg.getAllRecipients());
170 bus.close();
171 }
172
173 /***
174 * Versendet eine eMail mit Anhang über einen SMTP Server.
175 * Based on a JavaMail Wrapper example from http://w3.iac.net/~crawford/ .
176 *
177 * @param smtp_host Der Name des zu verwendenenden SMTP Servers
178 * @param smtp_port Der Port des zu verwendenenden SMTP Servers
179 * @param smtp_user Der Benutzername für den Versand über den SMTP Server
180 * @param smtp_password Das Passwort für den Versand über den SMTP Server
181 * @param from Der Absender (Absenderadresse)
182 * @param to Der Adressat (Zieladresse)
183 * @param subject Der Betreff
184 * @param message Der Inhalt
185 * @param attach Die Attachments
186 *
187 * @throws IOException
188 * @throws AddressException
189 * @throws MessagingException
190 * @throws AuthenticationFailedException
191 * @throws SendFailedException
192 */
193 public static final void sendWithAttachments(String smtp_host, String smtp_port, String smtp_user, String smtp_password, String from, String to, String cc, String subject, String message, Vector attach) throws IOException, AddressException, MessagingException, AuthenticationFailedException, SendFailedException
194 {
195 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
196
197 String proxyHost = properties.getProperty("ProxyHost");
198 String proxyPort = properties.getProperty("ProxyPort");
199
200 if(proxyHost != null && proxyHost != "" && proxyPort != null && proxyPort != "" )
201 {
202 Properties systemSettings = System.getProperties();
203 systemSettings.put("proxySet", "true");
204 systemSettings.put("proxyHost", proxyHost);
205 systemSettings.put("proxyPort", proxyPort);
206 System.setProperties(systemSettings);
207 }
208
209 Properties props = new Properties();
210
211 if(smtp_user == null)
212 smtp_user = "";
213 if(smtp_password == null)
214 smtp_password = "";
215 if(smtp_host == null)
216 smtp_host = "";
217 if(smtp_port == null)
218 smtp_port = "";
219
220 if(smtp_user != null && smtp_user != "" && smtp_password != null && smtp_password != "" )
221 props.put("mail.smtp.auth", "true");
222 props.put("mail.smtp.localhost", "CabaWeb Server");
223 props.put("mail.smtp.user", smtp_user.trim());
224 props.put("mail.smtp.host", smtp_host.trim());
225 props.put("mail.smtp.port", smtp_port.trim());
226
227 Session session = Session.getDefaultInstance(props, null);
228
229 Message msg = prepareHeader(session, from, to, cc, subject);
230
231 MimeMultipart mp = new MimeMultipart();
232
233 MimeBodyPart text = new MimeBodyPart();
234 text.setDisposition(Part.INLINE);
235 text.setContent(message, "text/plain");
236 mp.addBodyPart(text);
237
238 for (int i = 0; i < attach.size(); i++)
239 {
240 MimeBodyPart file_part = new MimeBodyPart();
241 File file = (File) attach.elementAt(i);
242 FileDataSource fds = new FileDataSource(file);
243 DataHandler dh = new DataHandler(fds);
244 file_part.setFileName(file.getName());
245 file_part.setDisposition(Part.ATTACHMENT);
246 file_part.setDescription("Attached file: " + file.getName());
247 file_part.setDataHandler(dh);
248 mp.addBodyPart(file_part);
249 }
250
251 msg.setContent(mp);
252 msg.saveChanges();
253
254 Transport bus = session.getTransport("smtp");
255
256 bus.connect(smtp_host.trim(), Integer.parseInt(smtp_port.trim()), smtp_user.trim(), smtp_password.trim());
257
258 bus.sendMessage(msg, msg.getAllRecipients());
259 bus.close();
260 }
261
262 /***
263 * Versendet eine eMail an den Admin mit den übergebeben Strings.
264 *
265 * @param subject Der Betreff
266 * @param message Der Inhalt
267 *
268 * @return String Fehlernachricht
269 */
270 public static final String SendErrorMailToAdmin(String subject, String message)
271 {
272 String smtphost = null;
273 String smtpport = null;
274 String smtpuser = null;
275 String smtppassword = null;
276 String absenderadresse = null;
277 String adminadresse = null;
278
279 try
280 {
281 Properties properties = StringUtilities.getPropertiesFromFile("cabaweb.properties");
282
283 smtphost = properties.getProperty("SMTPHost");
284 smtpport = properties.getProperty("SMTPPort");
285 smtpuser = properties.getProperty("SMTPUser");
286 smtppassword = properties.getProperty("SMTPPassword");
287 absenderadresse = properties.getProperty("AbsenderAdresse");
288 adminadresse = properties.getProperty("AdminAdresse");
289
290 MailUtilities.sendMail(smtphost, smtpport, smtpuser, smtppassword, absenderadresse, adminadresse, null, subject, message);
291 }
292 catch (IOException e)
293 {
294 return ("IOException - Failure sending mail to " + adminadresse);
295 }
296 catch (AuthenticationFailedException e)
297 {
298 return ("AuthenticationFailedException - Failure sending mail to " + adminadresse);
299 }
300 catch (SendFailedException e)
301 {
302 return ("SendFailedException - Failure sending mail to " + adminadresse);
303 }
304 catch (AddressException e)
305 {
306 return ("AddressException - Failure sending mail to " + adminadresse);
307 }
308 catch (MessagingException e)
309 {
310 return ("MessagingException - Failure sending mail to " + adminadresse);
311 }
312
313 return (null);
314 }
315
316 }